//输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。 
//
// 示例1： 
//
// 输入：1->2->4, 1->3->4
//输出：1->1->2->3->4->4 
//
// 限制： 
//
// 0 <= 链表长度 <= 1000 
//
// 注意：本题与主站 21 题相同：https://leetcode-cn.com/problems/merge-two-sorted-lists/ 
// Related Topics 递归 链表 👍 242 👎 0

package leetcode.editor.cn;

class HeBingLiangGePaiXuDeLianBiaoLcof {
    public static void main(String[] args) {
        Solution solution = new HeBingLiangGePaiXuDeLianBiaoLcof().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution {
       /* public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode dummy1 = new ListNode(-1);
            ListNode dummy2 = new ListNode(-1);
            dummy1.next = l1;
            dummy2.next = l2;
            ListNode cur1 = l1;
            ListNode cur2 = l2;
            // 输入：1->2->4, 1->3->4
            // 输出：1->1->2->3->4->4
            while (cur2.next != null && cur1.next != null) {
                while (cur2.next.val >= cur1.next.val) {
                    cur1 = cur1.next;
                }

                ListNode temp = cur2.next;
                cur2.next = cur1.next;
                cur1.next = cur2;
                cur2 = temp;
            }

            return dummy1.next;
        }*/

        /**
         * 新创建节点
         *
         * @param l1
         * @param l2
         * @return
         */
       /* public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1.next == null) {
                return l2;
            }
            if (l2.next == null) {
                return l1;
            }
            ListNode dummy = new ListNode(-1);
            ListNode cur = dummy;
            while (l1.next != null && l2.next != null) {
                if (l1.val > l2.val) {
                    cur.next = l2;
                    l2 = l2.next;
                } else {
                    cur.next = l1;
                    l1 = l1.next;
                }
                cur = cur.next;
            }
            if (l1.next == null) {
                cur.next = l2;
            } else {
                cur.next = l1;
            }
            return dummy.next;
        }*/

        /**
         * 递归法
         *
         * @param l1
         * @param l2
         * @return
         */
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null) {
                return l2;
            } else if (l2 == null) {
                return l1;
            } else {
                if (l1.val < l2.val) {
                    l1.next = mergeTwoLists(l1.next, l2);
                    return l1;
                } else {
                    l2.next = mergeTwoLists(l1, l2.next);
                    return l2;
                }
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
